<template>
	<view class="app">
		<view class="price-box">
			<text>支付金额</text>
			<text class="price">{{info.payMoney}}</text>
		</view>

		<view class="pay-type-list">
			<view class="type-item b-b" @click="changePayType(1)" style="border-bottom: 1rpx solid #f5f5f5;">
				<u-icon name="weixin-circle-fill" size="38" color="#36cb59"></u-icon>
				<view class="con">
					<text class="tit" :style="{'color':templateConfig.page.themeColor}">微信支付</text>
					<text>使用微信支付</text>
				</view>
				<label class="radio">
					<radio value="" :color="templateConfig.page.themeColor" :checked='payWay == 1' />
					</radio>
				</label>
			</view>
			<view class="type-item b-b" @click="changePayType(4)">
				<u-icon name="checkmark-circle" size="38" color="#ff633d"></u-icon>
				<view class="con">
					<text class="tit" :style="{'color':templateConfig.page.themeColor}">余额支付</text>
					<text>可用余额:{{userInfo.balance}}</text>
				</view>
				<label class="radio">
					<radio value="" :color="templateConfig.page.themeColor" :checked='payWay == 4' />
					</radio>
				</label>
			</view>
			
			
			
		</view>
		
		<text class="mix-btn" :style="{'background':templateConfig.page.themeColor}" @click="confirm">确认支付</text>
	</view>
</template>

<script>
import { appletsWechatOrderPay,partnerAppletsWechatOrderPay,getPerformance,getUserSubmitFormDataList,searchUserInfoById,balanceOrderPay } from '../../static/utils/api.js';
	export default {
		data() {
			return {
				templateConfig: uni.getStorageSync('templateConfig'),
				payWay: 1,
				nowBusinessInfo:{},
				orderInfo: {},
				info:{},
				formData:{},
				userInfo: uni.getStorageSync('userInfo'),
			};
		},
		computed: {
		
		},
		onLoad(options) {
			this.info = JSON.parse(decodeURIComponent(options.info));
			this.nowBusinessInfo = uni.getStorageSync('nowBusinessInfo');
			console.log(this.info);
		},
		onShow() {
			this.getuserInfo()
		},
		methods: {
			//选择支付方式
			changePayType(type) {
				this.payWay = type;
			},
			// 获取个人信息
			getuserInfo() {
				let data = {
					userId: uni.getStorageSync('userInfo').id
				}
				searchUserInfoById(data).then(res => {
					if (res.code == 200) {
						this.userInfo = res.body;
						uni.setStorageSync('userPayJson', res.body.userPayJson)
					}
				})
			},
			//确认支付
			confirm() {
				if(this.payWay==1){
					let postData = {
						client: '小程序',
						openId: uni.getStorageSync('openId'),
						orderId: this.info.id,
						orderNo: this.info.orderNo,
						payMoney:this.info.payMoney,
						payWay: 1,
						payType: 0,
						businessId: this.nowBusinessInfo.businessId.toString()
					};
					
					console.log(postData, '微信支付');
					uni.showLoading({
						title: '支付准备中',
						mask: true
					});
					partnerAppletsWechatOrderPay(postData).then(res=>{
						if(res.code==200){
							let payList=res.body
							let data = res.body;
							uni.hideLoading();
							wx.requestPayment({
								appId: payList.appid,
								nonceStr: payList.noncestr,
								package: payList.package,
								signType: 'RSA',
								timeStamp: payList.timestamp,
								paySign: payList.sign,
								success: function(res) {
									uni.hideLoading();
									uni.showToast({
										title: '微信支付成功'
									});
									setTimeout(function() {
										uni.redirectTo({
											url: '/pages/order/paysuccess'
										})
									}, 600);
								},
								fail: function(err) {
									uni.hideLoading();
									uni.showModal({
										title: '支付失败',
										content: '订单支付失败，请重新进行支付。如对订单金额有疑问请联系客服人员',
										showCancel: false
									});
									return;
								}
							});
						}
					})
				}else if(this.payWay==4){
					let that = this;
					if (that.userInfo.balance < that.info.payMoney) {
						uni.showModal({
							title: '提示',
							content: '余额不足,请充值',
							success: function(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/packageA/user/membercz'
									})
								} else if (res.cancel) {}
							}
						});
					}
					uni.showLoading({
						title: '支付准备中',
						mask: true
					});
					let postdata = {
						client: '小程序',
						openId: uni.getStorageSync('openId'),
						orderId: that.info.id,
						orderNo: that.info.orderNo,
						balancePayMoney:that.info.payMoney,
						payWay: 4,
						payType: 0,
						businessId: that.nowBusinessInfo.businessId.toString()
					}
					
					balanceOrderPay(postdata).then(res => {
						if (res.code == 200) {
							uni.hideLoading();
							uni.showToast({
								title: '余额支付成功'
							});
							setTimeout(function() {
								uni.redirectTo({
									url: '/pages/order/paysuccess'
								})
							}, 600);
						} else {
							uni.hideLoading();
							uni.showToast({
								title: res.msg
							});
						}
					})
					
				}
					
			},
			
			// 余额支付
			balancePay() {
				let that = this;
				if (that.balanceMoney < that.orderInfo.payMoney) {
					uni.showModal({
						title: '提示',
						content: '余额不足,请充值',
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/index/membercz'
								})
							} else if (res.cancel) {}
						}
					});
				}
				let postdata = {
					"client": "小程序",
					"extParm": "",
					"openId": that.openId,
					"orderId": that.orderInfo.id,
					"orderNo": that.orderInfo.orderNo,
					"payMoney": that.orderInfo.payMoney,
					"payType": 0,
					"payWay": 4
				}
				uni.showLoading({
					title: '支付准备中',
					mask: true
				});
				that.Post2('/api/pay/balanceOrderPay', postdata).then(res => {
					if (res.code == 200) {
						uni.hideLoading();
						uni.showToast({
							title: '微信支付成功'
						});
						setTimeout(function() {
							let id = JSON.parse(JSON.stringify(that.orderInfo.id));
							uni.navigateTo({
								url: `/pages/order/paysuccess?id=${id}&&money=${that.orderInfo.payMoney}`
							});
						}, 600);
					} else {
						uni.hideLoading();
						uni.showToast({
							title: res.msg
						});
					}
				})
			},
		}
	}
</script>

<style lang='scss'>
	.app {
		width: 100%;
	}

	.price-box {
		background-color: #fff;
		height: 265upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;

		.price{
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;
			&:before{
				content: '￥';
				font-size: 40upx;
			}
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;
		
		.type-item{
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position:relative;
		}
		
		.icon{
			width: 100upx;
			font-size: 52upx;
		}
		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}
		.icon-weixinzhifu {
			color: #36cb59;
		}
		.icon-alipay {
			color: #01aaef;
		}
		.tit{
			font-size: 32rpx;
			color: #01aaef;
			margin-bottom: 4upx;
		}
		.con{
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: 26rpx;
			color: $uni-text-color;
			margin-left: 30rpx;
		}
	}
	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: 26rpx;
		color: #fff;
		background-color: #ff633d;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}

</style>
